<?php
namespace Users\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Expression;
class OffersTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	public function addoffer($addData,$exp_status)
    {
		$data = array(
			'offer_name'		=> 	$addData['offer_name'], 	
			'offer_amount'   	=> 	$addData['offer_amount'],
			'user_id'   	    => 	null,
			'coupon_code'   	=> 	null,
			'percent_discount'	=>  $addData['offer_discount'],
			'offer_expries'	    =>  $exp_status,
			'starting_date'     => 	$addData['start_date'], 
			'ending_date'	    => 	$addData['end_date'],
			'status' 		    => 	1,
			'delete_offer' 		=> 	0,
			
		);
		if($addData['hid_offer_id']!=''){	
			$data1 = array(
				'updated_date' 	 => date('Y-m-d H:i:s'),
			 );
			 $data3= array_merge($data, $data1);
			$this->tableGateway->update($data, array('offer_id' => $addData['hid_offer_id']));
		}else{
			$data1 = array(
				'added_date' 	 => date('Y-m-d H:i:s'),		
				'updated_date' 	 => date('Y-m-d H:i:s'),
			 );
			  $data3=array_merge($data, $data1);
			$this->tableGateway->insert($data3);
		}
		return $this->tableGateway->lastInsertValue;
    }	
	public function getcount($data='')
	{
		$select = $this->tableGateway->getSql()->select();
		if(isset($data['sSearch'])){
			if($data['sSearch']!='0' && $data['sSearch']!='')
			{
				$select->where(array(
					new Predicate\PredicateSet(
						array(
						new Predicate\Like('offer_name', '%'.$data['sSearch'].'%'),
						),
							Predicate\PredicateSet::COMBINED_BY_OR
					),
				));
			}
		}
		$resultSet = $this->tableGateway->selectWith($select);		
		return $resultSet->count();
	}
	public function getoffersGrid($searchkey,$aColumns,$data,$paginated=false)
	{
		$select = $this->tableGateway->getSql()->select();	
		// Search
		if(isset($data['sSearch'])){
			if($data['sSearch']!='0' && $data['sSearch']!='')
			{
				$select->where(array(
					new Predicate\PredicateSet(
						array(
						new Predicate\Like('offer_name', '%'.$data['sSearch'].'%'),
						),
							Predicate\PredicateSet::COMBINED_BY_OR
					),
				));
			}
		}
		// End		
		// Sort Order
		if(isset($data['iSortCol_0']))
		{
			$sOrder="";
			for($i=0;$i<intval($data['iSortingCols']);$i++)
			{
				if($data['bSortable_'.intval($data['iSortCol_'.$i])] == "true")
				{
					$sOrder.=$aColumns[intval($data['iSortCol_'.$i])]." ".$data['sSortDir_'.$i].",";
				}
			}
			$sOrder = substr_replace($sOrder,"", -1);
		}		
		if($sOrder!=''){
			$select->order($sOrder);
		}
		// End		
		// Pagination
		if(isset($data['iDisplayStart']) && $data['iDisplayLength'] != '-1')
		{
			$start=(int)$data['iDisplayStart'];
			$currentPage = (int)$data['iDisplayLength'];
			$resultsPerPage = (int) $currentPage;
			$select->order('offer_id DESC')->limit($currentPage)->offset($start);
		}
		// End	
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
	public function editoffer($offer_id){
		$select = $this->tableGateway->getSql()->select();			
		$select->where('offer_id="'.$offer_id.'"');	
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet->current();		
	}
	public function changeStatus($userIds,$value)
	{
		$data = array(
			'status'       =>$value,
		);
		$updateStatus=$this->tableGateway->update($data,array('(offer_id IN ('.$userIds.'))'));
		return 	$updateStatus;			
	}
	public function expireddateStatus($offerid){
		$data = array(
			'delete_offer' 	 => 1,
			'updated_date' 	     =>   date('Y-m-d H:i:s'),
		 );	
		$result=$this->tableGateway->update($data, array('offer_id'=>$offerid));
		return $result;
	}
	public function getallofferRecords(){
		$select = $this->tableGateway->getSql()->select();
		$resultSet = $this->tableGateway->selectWith($select);
        return $resultSet;		
	}
}